<?php
defined('IN_IA') or exit('Access Denied');
class Xuan_zyy_Poster
{

     /**
    *   生成海报
    **/
    public function createPoster($params) {
        global $_W;
        $poster_id = intval($params['poster_id']);
        if (empty($poster_id)) {
            return false;
        }
        $ext_info = pdo_fetchcolumn("SELECT ext_info FROM ".tablename('xuan_hb_poster_data').' WHERE id=:id', array(':id'=>$poster_id));
        $ext_info = json_decode($ext_info, 1);
        if (empty($ext_info) || empty($ext_info['back'])) {
            return false;
        }

        $bgpath = $_W['attachurl']. $ext_info['back'];
        $info=getimagesize($bgpath);
        $type=image_type_to_extension($info[2],false);
        $create="imagecreatefrom{$type}";
        $putimage="image{$type}";
        $bgpng = $create($bgpath);

        $font = "../addons/xuan_zyy/public/msyh.ttf";

        if (!empty($ext_info['poster']['qr'])) {

            //二维码
            //$tmplogo = "../addons/xuan_zyy/public/poster/base.jpg";

            $tmplogo='qr.jpg';
            require_once(IA_ROOT.'/framework/library/qrcode/phpqrcode.php');
            QRcode::png($params['url'],$tmplogo,'L',15,2);
            $QR = imagecreatefromstring(file_get_contents($tmplogo));
            $newQRcode = imagecreatetruecolor($ext_info['poster']['qr']['width'],$ext_info['poster']['qr']['height']);
            imagecopyresized($newQRcode,$QR,0,0,0,0,$ext_info['poster']['qr']['width'],$ext_info['poster']['qr']['height'],imagesx($QR),imagesy($QR));
            imagecopymerge($bgpng,$newQRcode,$ext_info['poster']['qr']['left'],$ext_info['poster']['qr']['top'],0,0,imagesx($newQRcode),imagesy($newQRcode),100);
        }

        if (!empty($ext_info['poster']['head'])) {
            //头像

            if (strstr($params['member']['avatar'], 'xuan_zyy') || strstr($params['member']['avatar'], 'attachment')) {
                $avatar = imagecreatefromstring(file_get_contents($params['member']['avatar']));
            } else {
                $avatar = imagecreatefromstring(curl_file_get_contents($params['member']['avatar']));
            }
            $newAvatar = imagecreatetruecolor($ext_info['poster']['head']['width'],$ext_info['poster']['head']['height']);
            imagecopyresized($newAvatar,$avatar,0,0,0,0,$ext_info['poster']['head']['width'],$ext_info['poster']['head']['height'],imagesx($avatar),imagesy($avatar));
            imagecopymerge($bgpng,$newAvatar,$ext_info['poster']['head']['left'],$ext_info['poster']['head']['top'],0,0,$ext_info['poster']['head']['width'],$ext_info['poster']['head']['height'],100);
        }

        if (!empty($ext_info['poster']['nickname'])) {
            //昵称
            $poster_color = hex2rgb($ext_info['poster']['nickname']['color']);
            $color = imagecolorallocatealpha($bgpng,$poster_color['r'],$poster_color['g'],$poster_color['b'],0);
            imagettftext($bgpng,$ext_info['poster']['nickname']['size'],0,$ext_info['poster']['nickname']['left'],$ext_info['poster']['nickname']['top'],$color,$font,$params['member']['nickname']);
        }

        if (!empty($ext_info['poster']['phone'])) {
            //电话
            $poster_color = hex2rgb($ext_info['poster']['phone']['color']);
            $color = imagecolorallocatealpha($bgpng,$poster_color['r'],$poster_color['g'],$poster_color['b'],0);
            imagettftext($bgpng,$ext_info['poster']['phone']['size'],0,$ext_info['poster']['phone']['left'],$ext_info['poster']['phone']['top'],$color,$font,$params['member']['phone']);
        }

        if (!empty($ext_info['poster']['title'])) {
            //自定义
            $poster_color = hex2rgb($ext_info['poster']['title']['color']);
            $color = imagecolorallocatealpha($bgpng,$poster_color['r'],$poster_color['g'],$poster_color['b'],0);
            imagettftext($bgpng,$ext_info['poster']['title']['size'],0,$ext_info['poster']['title']['left'],$ext_info['poster']['title']['top'],$color,$font,$params['member']['title']);
        }



        $res = $putimage($bgpng,$params['out']);
        @imagedestroy($QR);
        imagedestroy($bgpng);


    }
}